<?php
/*
Auth:Sang
Desc:支付方式模型
Date:2014-10-27
*/
namespace Mod;
use Lib\Model;
class Payment extends Model{
    public function addPayment($data){
        if(empty($data) || !is_array($data)){
            return false;
        }
        
        $validator = new \Lib\Validator($data);
        $validator->setRules([
            'payment_code' => ['required'=>true,'validate'=>'trim'],
            'payment_name' => ['required'=>true,'validate'=>'trim'],
            'config' => ['required'=>true,'validate'=>'trim'],
            'is_online' => ['validate'=>'intval,abs'],
            'enabled' => ['required'=>true,'validate'=>'intval,abs'],
            'sort_order' => ['validate'=>'intval,abs'],
        ])->setMessages([
            'payment_code' => ['required'=>'接口代码不能为空','validate'=>''],
            'payment_name' => ['required'=>'接口名称不能为空','validate'=>''],
            'config' => ['required'=>'接口配置不能为空','validate'=>''],
            'enabled' => ['required'=>'是否启用不能为空','validate'=>''],
        ]);
    
        $data = $validator->validate();
        if(!$data){
            $this->error = $validator->getError();
            return false;
        }
    
        return $this->insert($data);
    }

    public function delPayment($id){
        if(empty($id) || (!is_numeric($id) && !is_array())){
            return false;
        }
        $ids = is_array($id) ? join(',',$id) : $id;
        return $this->where("payment_id in({$ids})")->delete();
    }

    public function updatePayment($code,$data){
        if(empty($code) || empty($data) || !is_array($data)){
            return false;
        }
        if(isset($data['payment_code'])) unset($data['payment_code']);
        $validator = new \Lib\Validator($data);
        $validator->setRules([
            'payment_name' => ['required'=>true,'validate'=>'trim'],
            'config' => ['required'=>true,'validate'=>'trim'],
            'is_online' => ['validate'=>'intval,abs'],
            'enabled' => ['required'=>true,'validate'=>'intval,abs'],
            'sort_order' => ['validate'=>'intval,abs'],
        ])->setMessages([
            'payment_name' => ['required'=>'接口名称不能为空','validate'=>''],
            'config' => ['required'=>'接口配置不能为空','validate'=>''],
            'enabled' => ['required'=>'是否启用不能为空','validate'=>''],
        ]);
    
        $data = $validator->validate();
        if(!$data){
            $this->error = $validator->getError();
            return false;
        }
        return $this->where("payment_code = '{$code}'")->update($data);
    }

    public function disablePayment($code){
        return $this->where("payment_code='{$code}'")->update(['enabled'=>0]);
    }

    public function getPaymentById($id){
        if(empty($id) || !is_numeric($id)){
            return false;
        }
        return $this->where("payment_id={$id}")->fetch();
    }

    public function getPaymentByCode($code){
        if(empty($code)){
            return false;
        }
        return $this->where("payment_code='{$code}'")->fetch();
    }

    public function search($condition,$offset=0,$size=20,$order='payment_id desc'){
        return $this->where($condition)->order($order)->limit($offset,$size)->select();
    }

    public function searchCount($condition){
        return $this->where($condition)->count();
    }

    public function getConfig($code){
        $payment = $this->fields('config')->where("payment_code='{$code}'")->fetch();
        if($payment && $payment['config']){
            return unserialize($payment['config']);
        }
        return [];
    }
}